package com.example.excelimporter.controller;

import com.example.excelimporter.model.ImportStatistics;
import com.example.excelimporter.service.ImportStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/statistics")
@Tag(name = "Import Statistics", description = "API for accessing import statistics and analysis")
public class ImportStatisticsController {

    @Autowired
    private ImportStatisticsService statisticsService;

    @GetMapping("/template/{templateId}")
    @Operation(summary = "Get template statistics", description = "Retrieve statistics for a specific template")
    @ApiResponse(responseCode = "200", description = "Statistics retrieved successfully")
    @ApiResponse(responseCode = "404", description = "Template not found")
    public ResponseEntity<List<ImportStatistics>> getTemplateStatistics(
            @Parameter(description = "Template ID") @PathVariable String templateId) {
        return ResponseEntity.ok(statisticsService.getStatisticsByTemplateId(templateId));
    }

    @GetMapping("/table/{targetTable}")
    @Operation(summary = "Get table statistics", description = "Retrieve statistics for a specific target table")
    @ApiResponse(responseCode = "200", description = "Statistics retrieved successfully")
    @ApiResponse(responseCode = "404", description = "Table not found")
    public ResponseEntity<List<ImportStatistics>> getTableStatistics(
            @Parameter(description = "Target table name") @PathVariable String targetTable) {
        return ResponseEntity.ok(statisticsService.getStatisticsByTargetTable(targetTable));
    }

    @GetMapping("/template/{templateId}/table/{targetTable}")
    @Operation(summary = "Get combined statistics", description = "Retrieve statistics for a specific template and table combination")
    @ApiResponse(responseCode = "200", description = "Statistics retrieved successfully")
    @ApiResponse(responseCode = "404", description = "Template or table not found")
    public ResponseEntity<List<ImportStatistics>> getTemplateTableStatistics(
            @Parameter(description = "Template ID") @PathVariable String templateId,
            @Parameter(description = "Target table name") @PathVariable String targetTable) {
        return ResponseEntity.ok(statisticsService.getStatisticsByTemplateAndTable(templateId, targetTable));
    }

    @GetMapping("/template/{templateId}/table/{targetTable}/aggregated")
    @Operation(summary = "Get aggregated statistics", description = "Retrieve aggregated statistics for a template and table combination")
    @ApiResponse(responseCode = "200", description = "Aggregated statistics retrieved successfully")
    @ApiResponse(responseCode = "404", description = "Template or table not found")
    public ResponseEntity<Map<String, Object>> getAggregatedStatistics(
            @Parameter(description = "Template ID") @PathVariable String templateId,
            @Parameter(description = "Target table name") @PathVariable String targetTable) {
        return ResponseEntity.ok(statisticsService.getAggregatedStatistics(templateId, targetTable));
    }

    @GetMapping("/template/{templateId}/table/{targetTable}/trend")
    @Operation(summary = "Get trend analysis", description = "Retrieve trend analysis for a template and table combination")
    @ApiResponse(responseCode = "200", description = "Trend analysis retrieved successfully")
    @ApiResponse(responseCode = "404", description = "Template or table not found")
    public ResponseEntity<Map<String, Object>> getTrendAnalysis(
            @Parameter(description = "Template ID") @PathVariable String templateId,
            @Parameter(description = "Target table name") @PathVariable String targetTable,
            @Parameter(description = "Number of days to analyze") @RequestParam(defaultValue = "30") int days) {
        return ResponseEntity.ok(statisticsService.getTrendAnalysis(templateId, targetTable, days));
    }
} 